import java.net.*;
import java.io.*;

public class
EchoServerThread extends Thread
{
	protected Socket socket;
	protected PrintWriter outLog = null;
	String name;
	public EchoServerThread(Socket clientSocket, PrintWriter outLog)
	{
		this.socket = clientSocket;
		this.outLog = outLog;
		name = this.getName();
	}
	public void writeLog(String line){
		synchronized(outLog){
			outLog.println(line);
		}
	}
	public void run()
	{
		InputStream inp = null;
		BufferedReader brinp = null;
		DataOutputStream out = null;
		try{
			inp = socket.getInputStream();
			brinp = new BufferedReader(new InputStreamReader(inp));
			out = new DataOutputStream(socket.getOutputStream());
		}
		catch(IOException e){
			writeLog(name + ": Bd przy tworzeniu strumieni.");
			return;
		}
		catch(Exception e){
			writeLog(name + ": " + e);
			return;
		}
		String line;
		while(true){
			try{
				line = brinp.readLine();
				writeLog(name + ": Odczytano lini: " + line);
				if((line == null) || line.equals("quit")){
		writeLog(name + ": Zakoczenie pracy 
z klientem: " + socket);
					socket.close();
					return;
				}
				else{
					out.writeBytes(line + "\n\r");
					writeLog(name + ": Wysano lini: " + line);
				}
			}
			catch(IOException e){
				writeLog(name + ": Bd wejcia-wyjcia.");
				return;
			}
		}
	}
}
